import { fakeRegister } from '@/services/api';
import { registerValid, register } from '@/services/user';
import { setAuthority } from '@/utils/authority';
import { reloadAuthorized } from '@/utils/Authorized';

export default {
  namespace: 'register',

  state: {
    status: undefined,
  },

  effects: {
    *submit({ payload }, { call, put }) {
      const response = yield call(fakeRegister, payload);
      yield put({
        type: 'registerHandle',
        payload: response,
      });
    },
    *getCaptcha({ payload, callback },{ call }) {
      const response = yield call(registerValid, payload);
      callback&&callback(response.msg)
    },
    *register({ payload, callback },{ call, put }) {
      const response = yield call(register, payload);
      if(response.msg==='用户成功注册'){
        yield put({
        type: 'registerSuccess',
        payload: {
          'status': 'ok'
        },
      })
      }
      
    }
  },

  reducers: {
    registerHandle(state, { payload }) {
      setAuthority('user');
      reloadAuthorized();
      return {
        ...state,
        status: payload.status,
      };
    },
    registerSuccess(state, { payload }) {
      return {
        ...state,
        status: payload.status,
      };
    },
  },
};
